var e = require("@babel/runtime/helpers/interopRequireDefault");

require("@babel/runtime/helpers/Arrayincludes");

var t = e(require("@babel/runtime/regenerator")), n = require("@babel/runtime/helpers/asyncToGenerator"), o = require("./utils/tool"), i = require("./config"), a = e(require("././utils/global")), s = e(require("./http/taskApi")), r = require("./utils/api.js"), c = require("./http/userApi.js"), l = require("./utils/common.js"), u = (require("./utils/websocket.js"), 
new a.default()), g = new s.default();

!function() {
    var e = Page;
    Page = function(t) {
        t = Object.assign({
            nothing: function() {},
            onShareAppMessage: function() {
                return {
                    title: "线上摇盲盒，让潮玩更好玩",
                    path: "/pages/index/index?shareFrom=" + getApp().getUsername()
                };
            },
            returnOnTap: function() {
                getApp().returnUI();
            }
        }, t), e(t);
    };
    var t = Component;
    Component = function(e) {
        e.methods = Object.assign({
            nothing: function() {},
            closeOnTap: function() {
                this.setData({
                    show: !1
                }), this.triggerEvent("onClose");
            }
        }, e.methods), t(e);
    };
}(), App({
    global: u,
    homePath: "/pages/index/index",
    data: {
        systemInfo: {},
        loginInfo: {},
        hasUnReadMessage: !1,
        offlinePaySuccess: 0,
        sourceInfo: "微信小程序"
    },
    onLaunch: function(e) {
        u.getSystemInfo(), wx.getStorageSync("loginInfo") && u.setLoginInfo(wx.getStorageSync("loginInfo"));
        var t = this;
        if (wx.canIUse("getUpdateManager")) {
            var n = wx.getUpdateManager();
            n.onCheckForUpdate(function(e) {
                e.hasUpdate && (n.onUpdateReady(function() {
                    wx.showModal({
                        title: "更新提示",
                        content: "新版本已经准备好，是否重启应用？",
                        showCancel: !1,
                        success: function(e) {
                            e.confirm && n.applyUpdate();
                        }
                    });
                }), n.onUpdateFailed(function() {
                    wx.showModal({
                        title: "已经有新版本了~",
                        content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开~",
                        showCancel: !1
                    });
                }));
            });
        } else wx.showModal({
            title: "提示",
            content: "当前微信版本过低，无法使用自动更新功能，请升级到最新微信版本后重试。",
            showCancel: !1
        });
        wx.getSystemInfo({
            success: function(e) {
                -1 != e.system.indexOf("Android") ? (t.globalData.navHeight = e.statusBarHeight + 48, 
                t.globalData.winHeight = e.windowHeight - e.statusBarHeight + 48, t.globalData.winWidth = e.windowWidth) : (t.globalData.navHeight = e.statusBarHeight + 44, 
                t.globalData.winHeight = e.windowHeight - e.statusBarHeight + 44, t.globalData.winWidth = e.windowWidth);
            },
            fail: function(e) {
                console.log(e);
            }
        });
        var o = wx.getSystemInfoSync();
        -1 != o.system.indexOf("Android") ? o.titleBarHeight = 48 : o.titleBarHeight = 44, 
        o.navBarHeight = o.statusBarHeight + o.titleBarHeight, this.data.systemInfo = o;
        try {
            this.data.loginInfo = wx.getStorageSync("LoginInfo"), this.data.loginInfo && this.data.loginInfo.UserName && (this.clearLoginInfo(), 
            console.log("【登录缓存】检测到旧版缓存，已清除"));
        } catch (e) {}
        this.isLogin() && (this.downloadUserInfo(), this.linkSocket());
        var i = e.query.shareFrom;
        console.log("shareFrom：", i || "空", ", my:", this.getUsername() || "空"), i && i != this.getUsername() && g.shareFrom(i).then(function(e) {
            console.log("shareFrom-成功：", e);
        }).catch(function(e) {
            console.log("shareFrom-失败：", e);
        });
    },
    globalData: {
        ipw: "",
        limit: 0,
        timeout: 5e3,
        timeoutObj: null,
        serverTimeoutObj: null,
        callback: function() {},
        token: "",
        firstMusicStatus: !0,
        globalFormIds: null,
        navHeight: 0,
        winHeight: 0,
        winWidth: 0,
        host: "https://m.ap1983.com",
        ihost: r.getHost(),
        wxCode: null,
        userInfo: null,
        userData: null,
        shareInfo: null
    },
    linkSocket: function() {
        var e = wx.getStorageSync("token");
        this.globalData.ipw = "".concat(i.config.wss_url, "?token=").concat(e);
        var t = this;
        wx.connectSocket({
            url: t.globalData.ipw,
            success: function() {
                t.initEventHandle();
            }
        });
    },
    initEventHandle: function() {
        var e = this;
        wx.onSocketMessage(function(t) {
            if ("pong" == t.data) e.reset(), e.start(); else {
                console.log(t);
                var n = JSON.parse(t.data);
                e.globalData.callback(n);
            }
        }), wx.onSocketOpen(function() {
            console.log("WebSocket连接打开"), e.reset(), e.start();
        }), wx.onSocketError(function(t) {
            console.log(t), console.log("WebSocket连接打开失败"), e.reconnect();
        }), wx.onSocketClose(function(t) {
            console.log(t), console.log("WebSocket 已关闭！"), e.reconnect();
        });
    },
    reconnect: function() {
        var e = this;
        e.lockReconnect || (e.lockReconnect = !0, clearTimeout(e.timer), e.timer = setTimeout(function() {
            e.linkSocket(), e.lockReconnect = !1, console.log("次数:" + e.globalData.limit);
        }, 1e3), e.globalData.limit = e.globalData.limit + 1);
    },
    reset: function() {
        return clearTimeout(this.globalData.timeoutObj), clearTimeout(this.globalData.serverTimeoutObj), 
        this;
    },
    start: function() {
        this.randomWord(!1, 16);
        this.globalData.timeoutObj = setTimeout(function() {
            wx.sendSocketMessage({
                data: "ping",
                success: function() {}
            });
        }, this.globalData.timeout);
    },
    randomWord: function(e, t, n) {
        var o = "", i = t, a = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ];
        e && (i = Math.round(Math.random() * (n - t)) + t);
        for (var s = 0; s < i; s++) {
            o += a[Math.round(Math.random() * (a.length - 1))];
        }
        return o;
    },
    appScan: function() {
        var e = this, t = !(arguments.length > 0 && void 0 !== arguments[0]) || arguments[0];
        wx.scanCode({
            success: function(n) {
                if (console.log("【扫码】", n), n.path) {
                    var o = "/" + n.path;
                    e.isLogin() ? t ? wx.navigateTo({
                        url: o
                    }) : wx.redirectTo({
                        url: o
                    }) : wx.navigateTo({
                        url: "/pages/login/login?redirectTo=" + encodeURIComponent(o)
                    });
                } else e.showAlert("提示", "扫描错误：" + JSON.stringify(n));
            },
            fail: function(e) {}
        });
    },
    msgTimer: null,
    onShow: function() {
        this.checkForbidden(), null != this.msgTimer && (clearInterval(this.msgTimer), this.msgTimer = null), 
        this.msgTimer = setInterval(function() {}, 1e4);
    },
    isLogin: function() {
        return this.getUsername().length > 0;
    },
    getUsername: function() {
        return this.data.loginInfo && this.data.loginInfo.userInfo && this.data.loginInfo.userInfo.id ? this.data.loginInfo.userInfo.id : "";
    },
    getToken: function() {
        return this.data.loginInfo && this.data.loginInfo.token ? this.data.loginInfo.token : "";
    },
    saveUserInfo: function(e) {
        this.data.loginInfo.userInfo = e, wx.setStorageSync("LoginInfo", this.data.loginInfo);
    },
    updateMyRedDot: function() {},
    loginThenRedirectTo: function(e, t) {
        var n = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : 0, o = "/pages/login/login?fromUserID=" + n + "&redirectTo=" + encodeURIComponent(t);
        e ? wx.redirectTo({
            url: o
        }) : wx.navigateTo({
            url: o
        });
    },
    checkMessage: function() {
        var e = this;
        r.messageQuery(this.getUsername(), this.getToken()).then(function(t) {
            t.Info.Exist && !e.data.hasUnReadMessage && (e.data.hasUnReadMessage = !0, wx.showModal({
                title: t.Info.Title,
                showCancel: !1,
                content: t.Info.Content,
                confirmText: "去查看",
                success: function(e) {
                    e.confirm && wx.navigateTo({
                        url: "/pages/2020/message-list/message-list"
                    });
                }
            }));
        }).catch(function(e) {});
    },
    checkForbidden: function() {},
    showAlert: function(e, t) {
        "string" != typeof t && (t = JSON.stringify(t)), wx.showModal({
            title: e,
            showCancel: !1,
            content: t
        });
    },
    showConfirm: function(e, t, n) {
        var o = arguments.length > 3 && void 0 !== arguments[3] && arguments[3], i = arguments.length > 4 && void 0 !== arguments[4] ? arguments[4] : "确定", a = arguments.length > 5 && void 0 !== arguments[5] ? arguments[5] : "取消";
        wx.showModal({
            title: e,
            content: t,
            showCancel: o,
            cancelText: a,
            confirmText: i,
            success: function(e) {
                e.confirm ? n(1) : e.cancel && n(0);
            }
        });
    },
    showToast: function(e) {
        wx.showToast({
            title: e,
            icon: "none"
        });
    },
    showToastLong: function(e) {
        wx.showToast({
            title: e,
            icon: "none",
            duration: 2e3
        });
    },
    showLoading: function() {
        var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "加载中…";
        wx.showLoading({
            title: e,
            mask: !0
        });
    },
    returnUI: function() {
        getCurrentPages().length > 1 ? wx.navigateBack({
            delta: 1
        }) : wx.reLaunch({
            url: "/pages/index/index"
        });
    },
    clearLoginInfo: function() {
        wx.removeStorageSync("LoginInfo"), wx.removeStorageSync("token"), this.data.loginInfo = null;
    },
    gotoLogin: function() {
        wx.navigateTo({
            url: "/pages/login/login"
        });
    },
    contactKefu: function() {
        var e = this;
        return n(t.default.mark(function n() {
            var o, i;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return e.showLoading(), t.prev = 1, t.next = 4, c.service();

                  case 4:
                    o = t.sent, i = o.msg, wx.navigateTo({
                        url: "/pages/view-image/view-image?image=" + encodeURIComponent(i)
                    }), t.next = 12;
                    break;

                  case 9:
                    t.prev = 9, t.t0 = t.catch(1), console.log(t.t0);

                  case 12:
                    return t.prev = 12, wx.hideLoading(), t.finish(12);

                  case 15:
                  case "end":
                    return t.stop();
                }
            }, n, null, [ [ 1, 9, 12, 15 ] ]);
        }))();
    },
    isTabPage: function(e) {
        return e.includes("pages/index/index") || e.includes("pages/direct-buy/direct-buy") || e.includes("pages/vip-center/vip-center") || e.includes("pages/task/task") || e.includes("pages/my/my");
    },
    gotoPage: function(e) {
        if (this.isTabPage(e)) if (this.isLogin()) {
            if (e.includes("pages/direct-buy/direct-buy")) {
                var t = l.parseUrl(e);
                t.cateId && (this.data.directCateId = t.cateId);
            }
            wx.switchTab({
                url: e
            });
        } else this.loginThenRedirectTo(!1, e); else this.isLogin() ? wx.navigateTo({
            url: e
        }) : this.loginThenRedirectTo(!1, e);
    },
    bannerOnTap: function(e) {
        console.log(e);
        var t = e.advSkipContent;
        if (0 == e.advSkipType) this.gotoPage(t); else if (1 == e.advSkipType) wx.navigateTo({
            url: "/pages/webview/webview?title=&url=" + encodeURIComponent(t)
        }); else if (2 == e.advSkipType) {
            var n = getCurrentPages();
            n[n.length - 1].setData({
                bannerDialogInfo: e
            });
        }
    },
    wxLogin: function() {
        return new Promise(function(e, t) {
            wx.login({
                success: function(t) {
                    e(t);
                },
                fail: function(e) {
                    t(e);
                }
            });
        });
    },
    getMallProductPath: function(e) {
        return "/pages/direct-buy-details/direct-buy-details?productId=" + e + "&shareFrom=" + this.getUsername();
    },
    openMallProduct: function(e) {
        var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "", n = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : "";
        this.gotoPage(this.getMallProductPath(e, t, n));
    },
    uploadUserInfo: function(e) {
        var i = this;
        return n(t.default.mark(function n() {
            var a;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, (0, o.promising)(wx.login)();

                  case 2:
                    return a = t.sent, t.next = 5, c.updateUserInfo(a.code, e);

                  case 5:
                    a = t.sent, i.data.loginInfo.userInfo = a.data, wx.setStorageSync("LoginInfo", i.data.loginInfo);

                  case 8:
                  case "end":
                    return t.stop();
                }
            }, n);
        }))();
    },
    downloadUserInfo: function() {
        var e = this;
        return n(t.default.mark(function n() {
            var o;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, t.next = 3, c.userInfo();

                  case 3:
                    o = t.sent, e.saveUserInfo(o.data), t.next = 10;
                    break;

                  case 7:
                    t.prev = 7, t.t0 = t.catch(0), console.log(t.t0);

                  case 10:
                  case "end":
                    return t.stop();
                }
            }, n, null, [ [ 0, 7 ] ]);
        }))();
    }
});